﻿using SmartProduction.Domain;
using SmartProduction.Write.API.Approve.Job;
using SmartProduction.Write.API.Approve.Workflow;
using WorkflowCore.Interface;
using IApplicationLifetime = Microsoft.AspNetCore.Hosting.IApplicationLifetime;

namespace SmartProduction.Write.API.Extensions
{
    public static class ConfigureExtensions
    {
       public static  IApplicationBuilder UseWorkFlow(this IApplicationBuilder app)
        {
            var host = app.ApplicationServices.GetService<IWorkflowHost>();

            host.RegisterWorkflow<MultiLevelApprovalWorkflow, Approval>();
            host.RegisterWorkflow<JobLevelApprovalWorkflow, Approval>();
            host.Start();

            var applifeTime=app.ApplicationServices.GetService<IApplicationLifetime>();
            applifeTime.ApplicationStopping.Register(() =>
            {
                host.Stop();
            });
            return app;
        }
    }
}
